﻿using MMO_MANAGER.DAO;
using MMO_MANAGER.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;

namespace MMO_MANAGER.BUS
{
    class user_bus
    {
        private AccessData dao;
        private SQLiteDataReader dr;
        public user_bus()
        {
            dao = new AccessData();
        }


        public DataTable GetData()
        {
            string sql = "select * from userlist where active=@act";
            var param = new SQLiteParameter[1];
            param[0] = new SQLiteParameter("@act", DbType.Int16);
            param[0].Value = 1;

            DataTable dt = dao.GetDataSet(sql, param, false).Tables[0];
            return dt;
        }

        /// <summary>
        ///     Lấy DS theo tên
        ///     Lấy dữ liệu từ USER_LIST theo tên truy cap
        /// </summary>
        /// <returns>DataTable</returns>
        public DataTable LayDSUsers(userlist nd)
        {
            string sql = "select * from userlist WHERE tendn=@ten and active=@act";
            var param = new SQLiteParameter[2];
            param[0] = new SQLiteParameter("@ten", DbType.String);
            param[0].Value = nd.tendn;
            param[1] = new SQLiteParameter("@act", DbType.Int16);
            param[1].Value = 1;

            DataTable dt = dao.GetDataSet(sql, param, false).Tables[0];
            return dt;
        }


        #region Dang nhap

        public int DangNhap(userlist nd)
        {
            DataTable dt = LayDSUsers(nd);

            if (dt.Rows.Count == 0)
                return 0;

            String m_SysPass = dt.Rows[0]["pwd"].ToString();

            if (m_SysPass != nd.pwd)
                return 1;
            else
            {
                nd.tendn = dt.Rows[0]["tendn"].ToString();
                nd.role = Convert.ToInt16(dt.Rows[0]["role"].ToString());
                nd.tenht = dt.Rows[0]["tenht"].ToString();
                return 2;
            }
        }

        #endregion

        #region Đổi mật khẩu, thiết lập lại kết nối

        public void ChangePassword(userlist nd)
        {
            string sql = "Update userlist SET pwd = @pass where tendn = @tendn";
            var param = new SQLiteParameter[2];
            param[0] = new SQLiteParameter("@tendn", DbType.String);
            param[0].Value = nd.tendn;

            param[1] = new SQLiteParameter("@pass", DbType.String);
            param[1].Value = nd.pwd;

            dao.ExecuteNoneQuery(sql, param);
        }

        #endregion

        // - Add : role: 2 | 1:admin, 2:user, 0:disable
        public void Insert(userlist us)
        {
            string sql = "Insert Into userlist (tendn, pwd, tenht, ngaysinh, diachi, sdt, email, role, active, ngayedit) " + 
                "values (@ten, @pwd, @tenht, @bir, @dc, @dt, @email, @role, @act, @ngayedit)";
            var param = new SQLiteParameter[10];
            param[0] = new SQLiteParameter("@ten", DbType.String);
            param[0].Value = us.tendn;
            param[1] = new SQLiteParameter("@pwd", DbType.String);
            param[1].Value = us.pwd;
            param[2] = new SQLiteParameter("@tenht", DbType.String);
            param[2].Value = us.tenht;
            param[3] = new SQLiteParameter("@bir", DbType.String);
            param[3].Value = us.ngaysinh;
            param[4] = new SQLiteParameter("@dc", DbType.String);
            param[4].Value = us.diachi;
            param[5] = new SQLiteParameter("@dt", DbType.String);
            param[5].Value = us.sdt;
            param[6] = new SQLiteParameter("@email", DbType.String);
            param[6].Value = us.email;
            param[7] = new SQLiteParameter("@role", DbType.Int16);
            param[7].Value = us.role;
            param[8] = new SQLiteParameter("@act", DbType.Int16);
            param[8].Value = us.active;
            param[9] = new SQLiteParameter("@ngayedit", DbType.DateTime);
            param[9].Value = us.ngayedit;
            dao.ExecuteNoneQuery(sql, param);
        }

        // - Sửa : không sửa pass, không sửa quyền
        public void Update(userlist us)
        {
            string sql = "Update userlist SET tenht=@ten, ngaysinh=@bir, diachi=@dc, dienthoai=@dt, email=@email, ngayedit=@ngayedit where tendn=@id";
            var param = new SQLiteParameter[7];
            param[0] = new SQLiteParameter("@id", DbType.String);
            param[0].Value = us.tendn;
            param[1] = new SQLiteParameter("@ten", DbType.String);
            param[1].Value = us.tenht;
            param[2] = new SQLiteParameter("@bir", DbType.String);
            param[2].Value = us.ngaysinh;
            param[3] = new SQLiteParameter("@dc", DbType.String);
            param[3].Value = us.diachi;
            param[4] = new SQLiteParameter("@dt", DbType.String);
            param[4].Value = us.sdt;
            param[5] = new SQLiteParameter("@email", DbType.String);
            param[5].Value = us.email;
            param[6] = new SQLiteParameter("@ngayedit", DbType.DateTime);
            param[6].Value = us.ngayedit;
            dao.ExecuteNoneQuery(sql, param);
        }

    }
}
